`
https://leetcode.cn/problems/minimize-the-maximum-difference-of-pairs/
`

/**
 * @param {number[]} nums
 * @param {number} p
 * @return {number}
 */
var minimizeMax = function (nums, p) {
  const n = nums.length
  if (n === 1) return 0

  nums.sort((a, b) => a - b)

  // 给定 mx ，能否不重复的选择至少 p 个数对，每个数对的绝对差值都 <= mx
  const check = (mx) => {
    let count = 0, i = 0
    while (i < n - 1) {
      if (nums[i + 1] - nums[i] <= mx) {
        // 选
        count++
        i += 2
      } else {
        // 不选
        i++
      }
    }
    return count >= p
  }

  let left = -1, right = nums.at(-1) - nums[0]
  while (left + 1 < right) {
    const mid = left + Math.floor((right - left) / 2)
    if (check(mid)) {
      right = mid
    } else {
      left = mid
    }
  }
  return right
};